using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Forecast.Business;

public partial class controls_grids_PredictionsActive : System.Web.UI.UserControl
{
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataView view = GridView1.DataSource as DataView;

        if (ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
        {
            if ("DESC" == ViewState["sortDirection"].ToString())
            {
                ViewState["sortDirection"] = "ASC";
            }
            else
            {
                ViewState["sortDirection"] = "DESC";
            }
        }
        else
        {
            ViewState["sortColumn"] = e.SortExpression.ToString();
            ViewState["sortDirection"] = "DESC";
        }

        view.Sort = ViewState["sortColumn"].ToString() + " " + ViewState["sortDirection"].ToString();
        useFirstRowColor = true;

        GridView1.DataSource = view;
        GridView1.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["sortColumn"] = " ";
            ViewState["sortDirection"] = " ";
        }

        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        DataTable table = activity.GetActivePredictions();
        DataView view = new DataView(table);
        view.Sort = "[" + ActivePredictions.Columns.EndDate.ToString() + "] DESC";

        GridView1.DataSource = view;
        GridView1.DataBind();

        if (GridView1.Rows.Count == 0)
        {
            _noGridText.Text = string.Format("<b>{0}</b><br/>", "There are currently no active predictions.");
            Panel1.Visible = false;
        }
        else
        {
            _noGridText.Visible = false;
        }
    }

    private bool useFirstRowColor = true;
    protected string GetChart(object allOffers)
    {
        string chartKey = Guid.NewGuid().ToString();

        int[] trades = (int[])allOffers;
        double[] tradesAsDouble = new double[trades.Length];
        int index = 0;
        foreach (int trade in trades)
        {
            tradesAsDouble[index++] = Convert.ToDouble(trade);
        }

        SparkLineData chartData = new SparkLineData();
        chartData.Data = tradesAsDouble;
        chartData.ImageWidth = 100;
        chartData.ImageHeight = 30;
        chartData.StdDev = false;

        if (useFirstRowColor)
        {
            chartData.AvgLineColor = System.Drawing.ColorTranslator.FromHtml("#E7E7FF");
            chartData.BgColor = System.Drawing.ColorTranslator.FromHtml("#E7E7FF");
        }
        else
        {
            chartData.AvgLineColor = System.Drawing.ColorTranslator.FromHtml("#F7F7F7");
            chartData.BgColor = System.Drawing.ColorTranslator.FromHtml("#F7F7F7");
        }
        useFirstRowColor = !useFirstRowColor;
        
        Session[chartKey] = chartData;
        return string.Format("<img src=\"{0}\" />",
            ResolveUrl("~/controls/charts/SparkLine.aspx") + "?data=" + chartKey);
    }

    protected string GetBidFormatted(object bidAsObject)
    {
        int bid = Int32.Parse(bidAsObject.ToString());
        if (bid == 0)
            return "- ";
        else return bid.ToString();
    }

    protected string GetAskFormatted(object askAsObject)
    {
        int ask = Int32.Parse(askAsObject.ToString());
        if (ask == 100)
            return "- ";
        else return ask.ToString();
    }

    protected string GetChangeFormatted(object oChange)
    {
        if (oChange == null)
            return string.Empty;

        double change = (double)oChange;

        return string.Format("<span style=\"color:{0};\">{1}{2}%</span>",            

            (change >= 0 ? "green" : "red"),
            (change >= 0 ? "+" : ""),
            change);
    }

    protected string GetPredictionArchive(string user)
    {
        if (user == "")
            return "";
        return "test";
        //return "See results of previous predictions in the <asp:HyperLink ID=\"HyperLink1\" runat=\"server\" NavigateUrl=\"~/OldPredictions.aspx\" Text=\"Prediction archive\" />.";
    }
    
    protected string GetTickerHeader(int tickerId, string tickerName)
    {
        if (Page.User.Identity.Name == "")
            return tickerName;
        else
            return string.Format("<a href=\"Prediction.aspx?id={0}\">{1}</a>", tickerId, tickerName);
    }

    protected string GetPrediction(int tickerId, string description)
    {
        if (Page.User.Identity.Name == "")
            return description;
        else
            return string.Format("<a href=\"Prediction.aspx?id={0}\">{1}</a>", tickerId, description);
    }
}
